Skip to content

Fix connection termination when using HTTP proxies#427

Closed
Claude wants to merge 2 commits intomainfrom
claude/fix-connection-termination-issue
Closed

Fix connection termination when using HTTP proxies#427
Claude wants to merge 2 commits intomainfrom
claude/fix-connection-termination-issue

Conversation

@Claude
Copy link
Copy Markdown
Contributor

@Claude Claude AI commented Mar 23, 2026

When using HTTP proxies (e.g., v2ray), rtp2httpd forwarded upstream response headers without ensuring Connection: close was present, causing proxies to keep connections alive indefinitely even after clients disconnected or switched channels.

Changes

  • Modified HTTP proxy response header handling (src/http_proxy.c:1027-1172)

    • Filter out upstream Connection headers in both redirect and normal response paths
    • Always inject Connection: close header before sending headers to clients
    • Ensures proper connection termination regardless of upstream behavior
  • Added e2e tests (e2e/test_http_proxy.py:500-592)

    • Verify Connection: close presence in normal responses
    • Verify Connection: close presence in redirect responses
    • Verify upstream keep-alive is correctly overridden with close

Technical Details

The fix modifies http_proxy_parse_response_headers() to parse and rebuild response headers, skipping any Connection header from upstream:

/* Filter out Connection header from upstream */
while (orig_line != NULL) {
  if (strncasecmp(orig_line, "Connection:", 11) != 0) {
    /* Copy other headers */
  }
  orig_line = strtok(NULL, "\r\n");
}

/* Always add Connection: close */
connection_queue_output(session->conn, (const uint8_t *)"Connection: close\r\n", 19);

This ensures HTTP proxies correctly terminate connections when clients disconnect, fixing the issue where switching channels or closing browsers left connections hanging.

Original prompt

This section details on the original issue you should resolve

<issue_title>系统代理环境下连接终止异常</issue_title>
<issue_description>在启用系统代理时,在自带网页播放器中换台甚至是关闭浏览器都无法终止连接。经观察未发现异常日志。

测试环境:
系统架构:X86_64
操作系统:Win10 22H2
浏览器:Chrome 146.0.7680.80
代理软件:V2rayN 7.18.0
代理模式:自动配置系统代理
代理路由:黑名单

关注到串流流量是通过直连。清除系统代理后,故障现象消失。

本人能力有限,暂不清楚导致该现象的原因。</issue_description>

<agent_instructions>尝试分析和修复。rtp2httpd 本身不支持 http keep-alive,预期每个客户端连接都是 short-lived,只有当客户端断开连接后才会释放资源(rtp2httpd 会发送 Connection: close)。是否由于用户设置了 http proxy,但是 http proxy 本身没有正确处理 Connection: close?

可以结合 https://github.com/2dust/v2rayn 源码,分析它是否正确支持 Connection: close

如果可以修复,需要补充 e2e 测试来复现本 issue 提出的问题。</agent_instructions>

Comments on the Issue (you are @claude[agent] in this section)

When using an HTTP proxy (like v2ray), rtp2httpd was forwarding upstream
response headers without ensuring Connection: close was present. This
caused HTTP proxies to keep connections alive even when clients
disconnected or switched channels.

Changes:
- Filter out Connection header from upstream responses
- Always inject Connection: close in responses to clients
- Add e2e tests to verify Connection: close header presence

Fixes issue where connections cannot be terminated when switching
channels or closing browser while using system proxy.

Co-authored-by: stackia <5107241+stackia@users.noreply.github.com>
Agent-Logs-Url: https://github.com/stackia/rtp2httpd/sessions/492e1c66-6b45-40a3-9efa-2f9757bb72a4
@Claude Claude AI changed the title [WIP] Fix connection termination issue under system proxy Fix connection termination when using HTTP proxies Mar 23, 2026
@Claude Claude AI requested a review from stackia March 23, 2026 11:29
@stackia
Copy link
Copy Markdown
Owner

stackia commented May 5, 2026

Closing this PR. After review, this fix targets the wrong problem and the change itself has no practical benefit.

Why this doesn't fix #418

Issue #418 is about an RTP multicast upstream (the reporter explicitly confirmed this in a follow-up comment). The RTP path does not go through src/http_proxy.c at all — RTP upstreams don't return HTTP response headers, so there are no "upstream response headers" to forward in the first place. This PR cannot affect the #418 scenario.

The likely root cause of #418 lies elsewhere — most plausibly in Chrome's socket-management behavior under PAC mode, where FIN delivery for "direct" connections may be deferred. The original reporter has also been unable to reproduce since.

Why injecting Connection: close here has no real benefit

For the HTTP proxy path itself, walking through the topology client ↔ V2Ray ↔ rtp2httpd ↔ upstream:

  1. After the response completes, worker_close_and_free_connection calls close(fd) (src/worker.c:160), so the TCP connection from rtp2httpd to the downstream proxy is always closed at the TCP layer, regardless of what the HTTP Connection header says.
  2. Whether V2Ray keeps its V2Ray ↔ client connection alive is V2Ray's internal business. Even if it does keep-alive that connection:
    • If the client reuses it for a new request, V2Ray must open a new TCP connection to rtp2httpd (the previous one is already closed). rtp2httpd just sees a fresh request — no leaked state, no resource retention.
    • If the client doesn't send a new request, V2Ray's own idle timeout reclaims it. Again, no impact on rtp2httpd.
  3. rtp2httpd itself does not support keep-alive (src/http.c:46-50 already forces Connection: close on its own responses), so there's no reuse on rtp2httpd's side either way.

So the worst case is purely a header-cleanliness inconsistency: the request side sends Connection: close but the response side passes through whatever upstream sent. There is no actual resource leak, dangling connection, or correctness issue on the rtp2httpd side.

Conclusion

  • Doesn't fix 系统代理环境下连接终止异常 #418 (wrong code path).
  • The standalone change is at most a cosmetic HTTP correctness tweak with no observable effect.
  • Adds non-trivial response-header rebuilding code on a hot path for no real gain.

Closing as not needed. The actual #418 root cause should be investigated client-side (Chrome NetLog under PAC mode + tcpdump on the rtp2httpd side to see when the client's FIN actually arrives).

@stackia stackia closed this May 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

系统代理环境下连接终止异常

2 participants